<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
    <!--地图单击获取经纬度-->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-family:"微软雅黑";}
        #allmap{width:200%;height:200px;}
        p{margin-left:5px; font-size:14px;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=l4aFlHGgQTTmK1fszpazz0naSkZo2A8K"></script>
    <title>地图单击事件</title>
</head>
<body>
<div style="width: 50%">
    <form  action="" id="school" onsubmit="return false" style="width:500px">
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i></a>
        <input type="text" id="id" name="id" value="{$data.id}" hidden>
        <input type="text" id="customer" name="customer_id" value="{$data.get_customer.id}" hidden>
        <input type="text" id="types" name="types" value="{$data.types}" hidden>
        <br>
        <div class="layui-form-item">
            <label class="layui-form-label" >学校名称</label>
            <div class="layui-input-block">
                <input type="text" id="name"  name="name" value="{$data.name}" required  lay-verify="required" placeholder="{$data.name}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label" >代理人姓名</label>
            <div class="layui-input-block">
                <input type="text"   value="{$data.get_customer.name}" required  lay-verify="required" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >登录帐号</label>
            <div class="layui-input-block">
                <input type="text" id="login"  name="login" required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >登录密码</label>
            <div class="layui-input-block">
                <input type="password" id="pwd"  name="password" required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >确认密码</label>
            <div class="layui-input-block">
                <input type="password" id="this_pwd"  name="this_pwd" required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >联系人</label>
            <div class="layui-input-block">
                <input type="text" id="linkman" name="phone_name" value="{$data.linkman}" required  lay-verify="required" placeholder="{$data.linkman}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >联系电话</label>
            <div class="layui-input-block">
                <input type="text" id="phone" name="phone" value="{$data.link}" required  lay-verify="required" placeholder="{$data.link}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >邮箱</label>
            <div class="layui-input-block">
                <input type="text" id="email"  name="email" value="{$data.email}" required  lay-verify="required" autocomplete="off" placeholder="{$data.email}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" >学校网址</label>
            <div class="layui-input-block">
                <input type="text" id="url"  name="url" value="{$data.url}" required  lay-verify="required"  autocomplete="off" placeholder="{$data.url}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">设备数量</label>
            <div class="layui-input-block">
                <input type="text" id="num"  name="num"  required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">终止时间</label>
            <div class="layui-input-block">
                <input type="text" id="endDate" name="end" class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">学校地区</label>
            <div class="layui-input-block" id="city">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详细地区</label>
            <div class="layui-input-block">
                <input type="text" id="address"  name="address"  required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">获取经纬度</label>
            <div class="layui-input-block">
                <div id="allmap"></div>
                经度:<input type="text" id="longitude" value="" name="longitude">
                <br>
                纬度:<input type="text" id="latitude" value="" name="latitude">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="sub" lay-submit lay-filter="*">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
</body>

</html>
<script type="text/javascript">
    // 百度地图API功能 经纬度
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
    function showInfo(e){
        $("#latitude").attr("value",e.point.lng);
        $("#longitude").attr("value",e.point.lat);
    }
    map.addEventListener("click", showInfo);
    // 百度地图API功能 缩放地图
    map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用


</script>

<script>
    layui.use(['laydate','layer','form'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        layer = layui.layer;
        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    });
</script>

<script>
    //日期插件
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#endDate' //指定元素
        });
    });
</script>
<script>
    //城市联动
    //默认1级
    $(function(){
        load(1);
    });
    //城市级联
    function load(id){
        $.ajax({
            url:"{:url('/school/regionFind')}",
            data:"id="+id,
            type:"get",
            dataType:"json",
            success:function(data){
                if(data == null || data.length == 0){
                    return ;
                }
                var info = "<select name='province[]'><option value=''>-请选择-</option>";
                for(var i=0;i<data.length;i++){
                    info+='<option value=\"'+data[i].id+'\">';
                    info+=data[i].region_name;
                    info+='</option>';
                }
                info += "</select>";
                $(info).appendTo("#city").change(function(){
                    $(this).nextAll("select").remove();
                    if($(this).val() != ""){
                        load($(this).val());
                    }
                });
            }
        });
    }
</script>
<script>
    //添加合作学校的信息
    $("#sub").click(function(){
        var name        = $("#name").val();
        var customer    = $("#customer").val();
        var pwd         = $("#pwd").val();
        var this_pwd    = $("#this_pwd").val();
        var phone_name  = $("#linkman").val();
        var phone       = $("#phone").val();
        var email       = $('#email').val();
        var url         = $('#url').val();
        var num         = $('#num').val();
        var city        = $('#city').val();
        var end         = $('#endDate').val();
        var address     = $('#address').val();
        var login       = $('#login').val();
        var longitude   = $('#longitude').val();
        var latitude    = $('#latitude').val();
        var types       = $('#types').val();

        //邮箱正则
        var isEmail = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
        //密码正则  只能输入6-20个字母、数字、下划线
        var patrn = /^(\w){6,20}$/;
        //手机号
        var mobile=/^[1][3,4,5,7,8][0-9]{9}$/;

        if(!name){
            layer.msg('请输入学校名称',{icon:5,time:2000});
            return;
        }

        if(!login){
            layer.msg('请输入学校账号',{icon:5,time:2000});
            return;
        }
        if(!patrn.test(login)){
            layer.msg('账号格式为8到20位任意字符',{icon:5,time:2000});
            return;
        }

        if(!address){
            layer.msg('请输入学校密码',{icon:5,time:2000});
            return;
        }

        if(!customer){
            layer.msg('请输入代理人id',{icon:5,time:2000});
            return;
        }

        if(!num){
            layer.msg('请输入需要的设备数量',{icon:5,time:2000});
            return;
        }

        if(!phone_name){
            layer.msg('请输入联系人姓名',{icon:5,time:2000});
            return;
        }

        if(!mobile.test(phone)){
            layer.msg('请输入正确的联系方式',{icon:5,time:2000});
            return;
        }

        if(!patrn.test(pwd)){
            layer.msg('密码格式为8到20位任意字符',{icon:5,time:2000});
            return;
        }

        if(pwd != this_pwd){
            layer.msg('两次密码不一致',{icon:5,time:2000});
            return;
        }

        if(email && !isEmail.test(email)){
            layer.msg('请输入正确的邮箱',{icon:5,time:2000});
            return;
        }
        if(!longitude || !latitude){
            layer.msg('请填写经纬度',{icon:5,time:2000});
            return;
        }
        if(!end){
            layer.msg('请选择到期时间',{icon:5,time:2000});
            return;
        }


        //如果type为1 那么添加学校，如果为2，那么添加到培训机构
        if(types == 2){
            $.ajax({
                url:"{:url('/customerTrain/addCustomerTrain')}",
                type:"post",
                data:$("#school").serialize(),
                dataType:"json",
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                        return;
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                        return;
                    }
                }
            });
        }
        if(types == 1){
            $.ajax({
                url:"{:url('/customerSchool/addCustomerSchool')}",
                type:"post",
                data:$("#school").serialize(),
                dataType:"json",
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                        return;
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                        return;
                    }
                }
            });
        }



    })
</script>